package com.zzy.excel;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class ReadExcel {
    public static void main(String[] args) {
        //readExcelXls2003();
        readExcelXlsx2007();
    }

    public static void readExcelXls2003() {//读 xls
        try {
            String src = "D:\\worksoft\\idea\\project\\springclouddemo\\zzy-util\\src\\main\\java\\com\\zzy\\excel\\test2003.xls";
            File file = new File(src);
            if (!file.exists()) {
                System.out.println("文件不存在");
                return;
            }
            FileInputStream fileInputStream = new FileInputStream(file);
            HSSFWorkbook workbook = new HSSFWorkbook(fileInputStream);
            HSSFSheet sheet = workbook.getSheetAt(0);
            HSSFRow row01 = sheet.getRow(0);
            List<String> titleList = new ArrayList<>();
            int maxRowNum = sheet.getLastRowNum();
            int maxCellNum = row01.getLastCellNum();
            for (int i = 0; i < maxCellNum; i++) {
                HSSFCell cell = row01.getCell(i);
                String v = "";
                if (cell != null) {
                    v = cell.getStringCellValue();
                }
                titleList.add(v);
            }
            System.out.println(titleList);


            for (int i = 1; i <= maxRowNum; i++) {
                HSSFRow row = sheet.getRow(i);

                for (int k = 0; k < maxCellNum; k++) {
                    HSSFCell cell = row.getCell(k);
                    String v = "";
                    if (cell != null) {
                        CellType cellType = cell.getCellType();
                        if (cellType != null) {
                            if (cellType.name().equals(CellType.STRING.name())) {
                                v = cell.getStringCellValue();
                            }
                            if (cellType.name().equals(CellType.NUMERIC.name())) {
                                v = cell.getNumericCellValue()+"";
                            }
                        }

                    }
                    System.out.print(v);
                }
                System.out.println( "" );
                System.out.println( "--------------------------------"  );
            }


        } catch (Exception e) {
            e.printStackTrace();
        }


    }

    public static void readExcelXlsx2007() {//读 xlsx 2007 和 easy poi、 冲突 只能二选一, 目前把  easy poi 注释掉了
        try {
            String src = "D:\\worksoft\\idea\\project\\springclouddemo\\zzy-util\\src\\main\\java\\com\\zzy\\excel\\test2007.xlsx";
            File file = new File(src);
            if (!file.exists()) {
                System.out.println("文件不存在");
                return;
            }
            FileInputStream fileInputStream = new FileInputStream(file);
            XSSFWorkbook workbook = new XSSFWorkbook(fileInputStream);
            XSSFSheet sheet = workbook.getSheetAt(0);
            XSSFRow row01 = sheet.getRow(0);
            List<String> titleList = new ArrayList<>();
            int maxRowNum = sheet.getLastRowNum();
            int maxCellNum = row01.getLastCellNum();
            for (int i = 0; i < maxCellNum; i++) {
                XSSFCell cell = row01.getCell(i);
                String v = "";
                if (cell != null) {
                    v = cell.getStringCellValue();
                }
                titleList.add(v);
            }
            System.out.println(titleList);


            for (int i = 1; i <= maxRowNum; i++) {
                XSSFRow row = sheet.getRow(i);

                for (int k = 0; k < maxCellNum; k++) {
                    XSSFCell cell = row.getCell(k);
                    String v = "";
                    if (cell != null) {
                        CellType cellType = cell.getCellType();
                        if (cellType != null) {
                            if (cellType.name().equals(CellType.STRING.name())) {
                                v = cell.getStringCellValue();
                            }
                            if (cellType.name().equals(CellType.NUMERIC.name())) {
                                v = cell.getNumericCellValue()+"";
                            }
                        }

                    }
                    System.out.print(v);
                }
                System.out.println( "" );
                System.out.println( "--------------------------------"  );
            }


        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
